//
//  LYouDetailVideoView.m
//  LYouSubscribeApp
//
//  Created by grx on 2018/12/14.
//  Copyright © 2018年 grx. All rights reserved.
//

#import "LYouDetailVideoView.h"

@implementation LYouDetailVideoView

- (instancetype)initWithVC:(UIViewController *)vc videoModel:(LYouVideoModel *)model {
    if (self = [super init]) {
        self.currenModel = model;
        [self addSubview:self.currentPlayView];
        [self.player playVideoWithView:self.currentPlayView.coverImgView url:model.video_url];
        }
    return self;
}

- (void)pause {
    
    [self.player pausePlay];
}

- (void)resume {
    [self.player resumePlay];
}

- (void)destoryPlayer {
    [self.player removeVideo];
}

//-(void)setModel:(LYouVideoModel *)model{
//    [self.videoBgImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.thumbnail_url]] placeholderImage:[UIImage imageNamed:@"placeholderimg"]];
//
//}

#pragma mark - GKDYVideoPlayerDelegate
- (void)player:(LYouVideoPlayer *)player statusChanged:(GKDYVideoPlayerStatus)status {
    switch (status) {
        case GKDYVideoPlayerStatusUnload:   // 未加载
            
            break;
        case GKDYVideoPlayerStatusPrepared:   // 准备播放
            break;
        case GKDYVideoPlayerStatusLoading: {     // 加载中
//            [self.videoBgImage removeFromSuperview];
        }
            break;
        case GKDYVideoPlayerStatusPlaying: {    // 播放中
//            [self.videoBgImage removeFromSuperview];
        }
            break;
        case GKDYVideoPlayerStatusPaused: {     // 暂停
//            [self.videoBgImage removeFromSuperview];
        }
            break;
        case GKDYVideoPlayerStatusEnded: {   // 播放结束
            // 重新开始播放
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.player resetPlay];
            });
        }
            break;
        case GKDYVideoPlayerStatusError:   // 错误
            
            break;
            
        default:
            break;
    }
}

- (void)player:(LYouVideoPlayer *)player currentTime:(float)currentTime totalTime:(float)totalTime progress:(float)progress {
    dispatch_async(dispatch_get_main_queue(), ^{
        //        [self.currentPlayView setProgress:progress];
    });
}

#pragma mark - 懒加载
- (LYouVideoPlayer *)player {
    if (!_player) {
        _player = [LYouVideoPlayer new];
        _player.delegate = self;
    }
    return _player;
}

- (LYouVideoControlView *)currentPlayView {
    if (!_currentPlayView) {
        _currentPlayView = [LYouVideoControlView new];
        _currentPlayView.favoriteBtn.hidden = YES;
        _currentPlayView.detailBtn.hidden = YES;
        _currentPlayView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        [_currentPlayView.coverImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", self.currenModel.thumbnail_url]] placeholderImage:[UIImage imageNamed:@"placeholderimg"]];

    }
    return _currentPlayView;
}

@end

